package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import bean.Cliente;
import bean.Dependente;

public class ClienteDAO 
{
	private static ClienteDAO instance = new ClienteDAO();
	public static ClienteDAO getInstance()
	{ return instance; }

	public Integer salvar(Session session, Cliente cli)
	{
		session.save(cli);
		return cli.getId();
	}
	
	@SuppressWarnings("unchecked")
	public Cliente buscarPorId(Session session, Integer id)
	{
		Cliente cliente = new Cliente();
		
		Criteria c = session.createCriteria(Cliente.class);
		c.add(Restrictions.eq("id", id));
		cliente = (Cliente) c.list().get(0);
		
		List<Dependente> listDependente = new ArrayList<Dependente>();
		listDependente = DependenteDAO.getInstance().buscarDependente(session, cliente);
		
		cliente.setDependentes(listDependente);
		
		return cliente;
	}
	
	@SuppressWarnings("unchecked")
	public List<Cliente> buscarPorNome(Session session, String nome)
	{
		List<Cliente> listCli = new ArrayList<Cliente>();
		
		Criteria c = session.createCriteria(Cliente.class);
		c.add(Restrictions.ilike("nome", nome, MatchMode.START));
		listCli = c.list();
		
		return listCli;
	}
	
	@SuppressWarnings("unchecked")
	public List<Cliente> buscarPorQtdeInfracoes(Session session, Integer min, Integer max)
	{
		List<Cliente> listCli = new ArrayList<Cliente>();
		Criteria c = session.createCriteria(Cliente.class);
		c.add(Restrictions.between("quantidadeInfracoes", (long)min, (long)max));
		listCli = c.list();
		
		return listCli;
	}
	
	
	public void atualizar(Session session, Cliente cli)
	{
		session.saveOrUpdate(cli);
	}
	
	public void excluir(Session session, Cliente cli)
	{
		session.delete(cli);
	}
}
